package com.cy.module.upload;

import com.cy.BasicController;
import com.cy.ext.plugin.cache.NoCache;
import com.cy.ext.plugin.route.ControllerBind;
import com.cy.utils.PropertyUtil;
import com.jfinal.upload.UploadFile;
import org.apache.commons.lang3.StringUtils;

@NoCache
@ControllerBind(controllerKey = "/res/file/upload")
public class FileUploadController extends BasicController{
	
	public void index() throws Exception{
		try{
			String flg = StringUtils.trim(getPara());
			String saveDir = "";
			if("1".equals(flg)){//APP版本
				saveDir = PropertyUtil.getGrosseValue(PropertyUtil.UPLOAD, "appDir");
			}
			if("2".equals(flg)){//固件版本
				saveDir = PropertyUtil.getGrosseValue(PropertyUtil.UPLOAD, "firmDir");
			}
			UploadFile uploadFile = getFile("Filedata", saveDir);
			
			String retDir = uploadFile.getSaveDirectory();
			String fileName = uploadFile.getFileName();
			
			String[] str = retDir.split("/");
			StringBuffer sb = new StringBuffer("");
			sb.append(PropertyUtil.getGrosseValue(PropertyUtil.UPLOAD, "host_domain"));
			sb.append(str[str.length - 1]);
			sb.append("/");
			sb.append(fileName);
			renderSuccess(sb.toString());
		}catch(Exception e){
		    e.printStackTrace();
			renderFail();
		}
	}
}
